package kr.jobtc.phonebook;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyHelper extends SQLiteOpenHelper {

	static final String db_name = "uiux4.db";
	static final int    db_version = 1;
	SQLiteDatabase db;
	
	public MyHelper(Context context) {
		super(context, db_name, null, db_version);
		try{
			db = getWritableDatabase();
		}catch(Exception ex){
			db = getReadableDatabase();
		}

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table member(_id integer primary key autoincrement, "+
					 " name text, phone text)";
		db.execSQL(sql);
		
		db.execSQL("insert into member(name, phone) values('aaaa1','11111')");
		db.execSQL("insert into member(name, phone) values('aaaa2','11112')");
		db.execSQL("insert into member(name, phone) values('aaaa3','11113')");
		db.execSQL("insert into member(name, phone) values('aaaa4','11114')");
		db.execSQL("insert into member(name, phone) values('aaaa5','11115')");
		db.execSQL("insert into member(name, phone) values('aaaa6','11116')");
		db.execSQL("insert into member(name, phone) values('aaaa7','11117')");
		db.execSQL("insert into member(name, phone) values('aaaa8','11118')");
		db.execSQL("insert into member(name, phone) values('aaaa9','11119')");
		db.execSQL("insert into member(name, phone) values('aaaa10','111110')");
		

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
	}
	
	public void insert(Data d){
		String sql = "insert into member(name, phone) values(?,?)";
		db.execSQL(sql,d.toArray());
	}
	
	
	public Cursor list(String where){
		String str[] = new String[1];
		str[0] = "%" + where + "%";
		String sql = "select * from member where name like ?";
		Cursor c= db.rawQuery(sql,str);
		
		return c;
	}
	
	public Data search(String[] name){
		Data d = new Data();
		String sql = "select * from member where name like ?";
		Cursor c = db.rawQuery(sql, name);
		if(c.moveToNext()){
			d.setPhone(c.getString(2));
		}
		
		return d;
	}

	public void modify(String[] str){
		String sql = "update member set phone=? where name=?";
		db.execSQL(sql, str);

	}
	public void delete(String[] str){
		String sql = "delete from member where name=?";
		db.execSQL(sql, str);
	}
	
}
